package cn.com.flower.passport.controller;

import cn.com.flower.common.pojo.authentication.CurrentPrincipal;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.security.core.annotation.AuthenticationPrincipal;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import springfox.documentation.annotations.ApiIgnore;

@Api(tags = "02.测试访问")
@RestController
@RequestMapping("/tests")
public class TestController {

    @GetMapping("/security/authenticate")
    @ApiOperation("基于Security执行认证")
    @ApiOperationSupport(order = 300)
    public String securityAuthenticate() {
        return "您已经通过了Spring Security的认证!";
    }

    @GetMapping("/security/principal")
    @ApiOperation("基于Security识别当事人")
    @ApiOperationSupport(order = 400)
    public String securityPrincipal(@ApiIgnore @AuthenticationPrincipal CurrentPrincipal principal) {
        return "您已经通过了Spring Security的认证!当事人ID: " + principal.getId()
                + "，当事人用户名: " + principal.getUsername();
    }

    @GetMapping("/security/authorize")
    @PreAuthorize("hasAuthority('/mall')") // 假设需要 /mall 权限
    @ApiOperation("基于Security处理授权")
    @ApiOperationSupport(order = 500)
    public String securityAuthorize() {
        return "您已经通过了Spring Security的认证与授权!";
    }

}
